package flc.ast.fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.PathUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import flc.ast.R;
import flc.ast.activity.PictureFormatActivity;
import flc.ast.activity.RecentFileActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.adapter.FileAdapter;
import flc.ast.bean.FileBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private FileAdapter fileAdapter;
    private int type;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivVideoGif.setOnClickListener(this);
        mDataBinding.ivVideoFormat.setOnClickListener(this);
        mDataBinding.ivPictureFormat.setOnClickListener(this);
        mDataBinding.ivAll.setOnClickListener(this);
        mDataBinding.ivVideo.setOnClickListener(this);
        mDataBinding.ivPicture.setOnClickListener(this);
        mDataBinding.tvMoreFile.setOnClickListener(this);

        mDataBinding.rvFileList.setLayoutManager(new GridLayoutManager(mContext, 3));
        fileAdapter = new FileAdapter();
        mDataBinding.rvFileList.setAdapter(fileAdapter);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivVideoGif://视频转gif
                SelVideoActivity.type = 0;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivVideoFormat://视频格式转换
                SelVideoActivity.type = 1;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivPictureFormat://图片格式转换
                getPermission();
                break;
            case R.id.ivAll://全部
                clearSelection();
                type = 0;
                mDataBinding.ivAll.setImageResource(R.drawable.dj_qb);
                getAllData();
                break;
            case R.id.ivVideo://视频
                clearSelection();
                type = 1;
                mDataBinding.ivVideo.setImageResource(R.drawable.dj_spa);
                getVideoData();
                break;
            case R.id.ivPicture://图片
                clearSelection();
                type = 2;
                mDataBinding.ivPicture.setImageResource(R.drawable.dj_tpa);
                getPictureData();
                break;
            case R.id.tvMoreFile://更多文件
                startActivity(RecentFileActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (type == 0) {
            getAllData();
        } else if (type == 1) {
            getVideoData();
        } else if (type == 2) {
            getPictureData();
        }
    }

    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_manage_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                startActivity(PictureFormatActivity.class);
            }
        }).request();
    }

    private void getAllData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                list.add(new FileBean(file.getPath()));
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getVideoData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                if (!ImageUtils.isImage(file)) {
                    list.add(new FileBean(file.getPath()));
                }
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void getPictureData() {
        List<FileBean> list = new ArrayList<>();
        //获取文件夹下文件
        String path = PathUtils.getFilesPathExternalFirst() + Content.myRecentFile;
        List<File> files = FileUtils.listFilesInDir(path);
        if (files.size() > 0) {
            for (File file : files) {
                if (ImageUtils.isImage(file)) {
                    list.add(new FileBean(file.getPath()));
                }
            }
        }
        if (list.size() > 0) {
            fileAdapter.setList(list);
            mDataBinding.rvFileList.setVisibility(View.VISIBLE);
            mDataBinding.ivFileNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvFileList.setVisibility(View.GONE);
            mDataBinding.ivFileNoData.setVisibility(View.VISIBLE);
        }
    }

    private void clearSelection() {
        mDataBinding.ivAll.setImageResource(R.drawable.dj_qba);
        mDataBinding.ivVideo.setImageResource(R.drawable.dj_sp);
        mDataBinding.ivPicture.setImageResource(R.drawable.dj_tp);
    }

}